package com.atguigu.admin.service.impl;

import com.atguigu.admin.mapper.SysOperLogMapper;
import com.atguigu.admin.model.entity.SysOperLog;
import com.atguigu.admin.model.vo.SysOperLogQueryVo;
import com.atguigu.admin.service.SysOperLogService;
import com.atguigu.common.log.service.OperLogService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 操作日志记录 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2022-10-29
 */
@Service
public class SysOperLogServiceImpl extends ServiceImpl<SysOperLogMapper, SysOperLog> implements SysOperLogService, OperLogService {


    @Override
    public void savaOperLog(SysOperLog sysOperLog) {
        baseMapper.insert(sysOperLog);
    }


    @Override
    public IPage<SysOperLog> selectPage(Long page, Long limit, SysOperLogQueryVo sysOperLogQueryVo) {

        Page<SysOperLog> pageParam = new Page<>(page, limit);
        String title = sysOperLogQueryVo.getTitle();
        String operName = sysOperLogQueryVo.getOperName();
        String createTimeBegin = sysOperLogQueryVo.getCreateTimeBegin();
        String createTimeEnd = sysOperLogQueryVo.getCreateTimeEnd();
        // 构造条件
        QueryWrapper<SysOperLog> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(title)) {
            wrapper.eq("title", title);
        }
        if (!StringUtils.isEmpty(operName)) {
            wrapper.eq("oper_name", operName);
        }
        if (!StringUtils.isEmpty(createTimeBegin)) {
            wrapper.ge("create_time", createTimeBegin);
        }
        if (!StringUtils.isEmpty(createTimeEnd)) {
            wrapper.le("create_time", createTimeEnd);
        }
        wrapper.orderByDesc("create_time");
        // 调用mapper方法
        return baseMapper.selectPage(pageParam, wrapper);

    }
}
